<template>
  <div>
    <el-card>
      <el-row>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item><a href="#">病人信息管理</a></el-breadcrumb-item>
          <el-breadcrumb-item>病人信息列表</el-breadcrumb-item>
        </el-breadcrumb>
      </el-row>
      <el-row>
        <el-input cleared v-model="queryInfo.query" ></el-input>
        <el-button type="primary" @click="getinformationList()">搜索</el-button>
        <el-button type="primary" @click="showAddDialog"
          >添加病人信息</el-button
        >
      </el-row>
      <el-empty description="暂无数据" v-if="informasionList.length==0"></el-empty>
      <el-table :data="informasionList" style="width: 100%" stripe border v-else >
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="name" label="姓名"> </el-table-column>
        <el-table-column prop="age" label="年龄"> </el-table-column>
        <el-table-column prop="sex" label="性别"> </el-table-column>
        <el-table-column prop="phone" label="电话"> </el-table-column>
        <el-table-column  label="主要症状" width = "600">
          <template slot-scope="scope">
            <el-tag v-for="item in scope.row.symptom" :key="item.id" type="warning">{{item}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否治愈">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isCure === '0'" type="danger"
              >未治愈</el-tag
            >
            <el-tag v-else type="success">已治愈</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="warning"
              size="small"
              @click="showEditDialog(scope.row._id)"
              >修改</el-button
            >
            <el-button
              type="danger"
              size="small"
              @click="deleteConfirm(scope.row._id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="添加病人信息"
      :visible.sync="addDialogVisible"
      width="50%"
    >
      <el-form
        :model="addForm"
        ref="addFormRef"
        label-width="100px"
        class="demo-ruleForm"
        :rules="FormRule"
      >
        <el-form-item label="姓名" prop = "name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop = "age">
          <el-input v-model="addForm.age"></el-input>
        </el-form-item>
        <el-form-item label="性别" >
          <el-radio v-model="addForm.sex" label="男">男</el-radio>
          <el-radio v-model="addForm.sex" label="女">女</el-radio>
        </el-form-item>
        <el-form-item label="电话"  prop = "phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="主要症状" prop = "symptom">
          <el-input v-model="addForm.symptom"></el-input>
        </el-form-item>
        <el-form-item label="是否治愈">
          <el-radio v-model="addForm.isCure" label="1">是</el-radio>
          <el-radio v-model="addForm.isCure" label="0">否</el-radio>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addConfirm">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="修改信息" :visible.sync="editDialogVisible" width="50%">
      <el-form
        :model="editForm"
        ref="editFormRef"
        label-width="100px"
        :rules="FormRule"
      >
        <el-form-item label="姓名"  prop = "name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
         <el-form-item label="年龄"  prop = "age">
          <el-input v-model="editForm.age"></el-input>
        </el-form-item>
         <el-form-item label="性别" >
          <el-radio v-model="editForm.sex" label="男">男</el-radio>
          <el-radio v-model="editForm.sex" label="女">女</el-radio>
        </el-form-item>
         <el-form-item label="电话" prop = "phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
         <el-form-item label="主要症状" prop = "symptom">
          <el-input v-model="editForm.symptom"></el-input>
        </el-form-item>
         <el-form-item label="是否治愈" >
           <el-radio v-model="editForm.isCure" label="1">是</el-radio>
          <el-radio v-model="editForm.isCure" label="0">否</el-radio>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editConfirm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      informasionList: [],
      total: 0,
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      addDialogVisible: false,
      addForm: {
        name: '',
        age: '',
        sex: '',
        phone: '',
        symptom: '',
        isCure: ''
      },
      addFormrules: [],
      editDialogVisible: false,
      editForm: {},
      FormRule: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        age: [
          { required: true, message: '请输入年龄', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
          // { type: 'number', message: '必须为数值'}
        ],
        phone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { min: 7, max: 11, message: '长度在 7 到 11 个字符', trigger: 'blur' }
          // { type: 'number', message: '必须为数值'}
        ],
        symptom: [
          { required: true, message: '请输入主要症状', trigger: 'blur' }
          // { min: 0, max: 100, message: '长度在 0 到 100 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getinformationList()
  },
  methods: {
    async getinformationList () {
      const doctorID = window.sessionStorage.getItem('doctorID')
      await this.$http.get('/api/profile/patientList/' + doctorID, { params: this.queryInfo }).then((res) => {
        if (res.status !== 200) return this.$message.error('获取信息失败')
        this.informasionList = res.data.information
        this.total = res.data.total
      })
        .catch(err => { this.$message.error('获取信息列表失败', err) })
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getinformationList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getinformationList()
    },
    showAddDialog () {
      this.addDialogVisible = true
    },
    addConfirm () {
      const doctorID = window.sessionStorage.getItem('doctorID')
      this.$refs.addFormRef.validate((valid) => {
        if (valid) {
          this.$http.post('/api/profile/add/' + doctorID, this.addForm).then((res) => {
            if (res.status === 200) {
              this.$message.success('添加病人信息成功')
              this.addDialogVisible = false
              this.getinformationList()
            } else {
            }
          })
        }
      })
    },
    async deleteConfirm (id) {
      const confirmResult = await this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消该操作')
      }
      this.$http.delete('/api/profile/delete/' + id)
        .then(res => {
          if (res.status !== 200) {
            this.$message.error('删除失败')
          }
        })
      this.$message.success('删除成功')
      this.getinformationList()
    },
    showEditDialog (id) {
      this.editDialogVisible = true
      this.$http.get('/api/profile/patientinfo/' + id)
        .then(res => {
          if (res.status !== 200) { return this.$message.error('获取信息失败') }
          const newForm = res.data
          newForm.symptom = newForm.symptom.join('/')
          this.editForm = res.data
        })
    },
    editConfirm () {
      this.$refs.editFormRef.validate(valid => {
        if (!valid) return
        this.$http.post('/api/profile/edit/' + this.editForm._id, this.editForm)
          .then(res => {
            if (res.status !== 200) return this.$message.error('修改信息失败')
            this.$message.success('修改信息成功')
            this.editDialogVisible = false
            this.getinformationList()
          })
      })
    }
  }
}
</script>

<style scoped>
.el-table {
  margin: 10px 0 40px 0;
}
.el-input {
  width: 300px;
  border: 1px solid lightblue;
}
.el-breadcrumb{
  margin-bottom: 20px;
}
.el-tag {
  margin: 0 5px;
}

</style>
